window.onload = function() {
    var container = document.querySelector('.container');
    var header = document.querySelector('.header');
    var content = document.querySelector('.content');
    var search_modal = document.querySelector('.-m-search-modal');
    var ctrl_search = document.querySelector('.J-ctrl-search');
    var input_key = document.querySelector('.-i-input-key');
    var btn_search = document.querySelector('.J-search');
    var history_list = document.querySelector('.history .list');
    var btn_del = document.querySelector('.J-del-record');

    var HISTORY_RECORD = 'history_record';

    content.style.marginTop = header.clientHeight - 16 + 'px';

    slide({
        container: ".content",
        loading: '.loading',
        next: function(e) {
            //松手之后执行逻辑,ajax请求数据，数据返回后隐藏加载中提示
            var that = this;
            setTimeout(function() {
                that.back.call();
            }, 2000);
        }
    });

    // 展示搜索记录
    function showHistory() {
        var _record = JSON.parse(localStorage.getItem(HISTORY_RECORD)) || [];
        var _html = '';

        for (var i = 0; i < _record.length; i++) {
            _html += "<li><span class='title J-record'>" + _record[i] + "</span></li>";
        }

        history_list.innerHTML = _html;
    };
    showHistory();

    // 点击输入框，弹出搜索层
    ctrl_search.onclick = function() {
        showHistory();
        search_modal.style.display = 'block';
    };

    // 点击搜索，保存搜索记录
    btn_search.onclick = function() {
        var _record = JSON.parse(localStorage.getItem(HISTORY_RECORD)) || [];
        var _key = input_key.value;
        var _key1 = input_key.value;
        var _is_exist = _record.filter(function(item) {
            return item == _key;
        }).length;
        if (_key != '' && _is_exist <= 0) {
            _record.push(_key);
            localStorage.setItem(HISTORY_RECORD, JSON.stringify(_record));
        }
       
        search_modal.style.display = 'none';
//        input_key.value = '';
        // 请求服务器查询
        document.getElementById('reportListId').submit();
    };

    // 删除搜索记录
    btn_del.onclick = function() {
        localStorage.setItem(HISTORY_RECORD, JSON.stringify([]));
        showHistory();
    };

    // 点击历史记录，执行查询
    history_list.onclick = function(e) {
    	var fphone = e.srcElement.innerHTML;
        if (e.target.className.indexOf('J-record') != -1) {
        	document.getElementById('phoneId').value == fphone;
        	window.location = "reportList?phone="+fphone;
        }
    };
};